//
//  PBServeListController.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/1/9.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBServeListController.h"
#import "PBServeListCell.h"
#import "PBServeListModel.h"
#import "PBServeBannerModel.h"
#import "PBAdsCell.h"
#import "PBServeDetailController.h"

static NSString * const kPBAdsCell = @"PBAdsCell";
static NSString * const kPBServeListCell = @"PBServeListCell";

@interface PBServeListController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray   *dataSource;
@property (nonatomic, strong) NSArray   *adsArray;
@end

@implementation PBServeListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}
- (void)configure {
    [self configureView];
    [self configureData];
}

- (void)configureView {
    [self.tableView registerNib:[UINib nibWithNibName:kPBAdsCell bundle:nil] forCellReuseIdentifier:kPBAdsCell];
    [self.tableView registerNib:[UINib nibWithNibName:kPBServeListCell bundle:nil] forCellReuseIdentifier:kPBServeListCell];
    self.tableView.tableFooterView = [[UIView alloc] init];
}
- (void)configureData {
    NSDictionary *param = @{@"cityId":[TMShareData shareData].cityId,
                            @"type":[NSString stringWithFormat:@"%zd",self.type+1]};
    [TMNetwork requestGetWithParam:param url:@"/banner/list" callback:^(TMBaseReqModel *model) {
        if (model.code == 200) {
            self.adsArray = [PBServeBannerModel objectArrayWithKeyValuesArray:model.data];
            [self getServeListData];
        }else {
            [self showHint:model.message];
        }
    }];
}

- (void)getServeListData {
    NSDictionary *param = @{@"type":[NSString stringWithFormat:@"%zd",self.type]};
    [TMNetwork requestGetWithParam:param url:@"/house/lifeService/list" callback:^(TMBaseReqModel *model) {
        if (model.code == 200) {
            self.dataSource = [PBServeListModel objectArrayWithKeyValuesArray:model.data];
            [self.tableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}


#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        PBAdsCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBAdsCell forIndexPath:indexPath];
        if (self.adsArray.count>0) {
            [cell refreshServeListWithArray:self.adsArray];
        }
        return cell;
    }
    PBServeListCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBServeListCell forIndexPath:indexPath];
    [cell refreshWithModel:self.dataSource[indexPath.row-1]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 190;
    }
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row > 0) {
        PBServeListModel *model = self.dataSource[indexPath.row-1];
        PBServeDetailController *vc = [[PBServeDetailController alloc] init];
        vc.title = model.name;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - super
- (void)resetNavigationBarItems {
    [super resetNavigationBarItems];
    if (self.type == 1) {
        self.title = @"搬家";
    }else if (self.type == 2) {
        self.title = @"保洁";
    }else if (self.type == 3) {
        self.title = @"维修";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
